/**
<p>输入一个矩阵，按照从外向里以顺时针的顺序依次打印出每一个数字。</p>

<p>&nbsp;</p>

<p><strong>示例 1：</strong></p>

<pre><strong>输入：</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]
<strong>输出：</strong>[1,2,3,6,9,8,7,4,5]
</pre>

<p><strong>示例 2：</strong></p>

<pre><strong>输入：</strong>matrix =&nbsp;[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
<strong>输出：</strong>[1,2,3,4,8,12,11,10,9,5,6,7]
</pre>

<p>&nbsp;</p>

<p><strong>限制：</strong></p>

<ul> 
 <li><code>0 &lt;= matrix.length &lt;= 100</code></li> 
 <li><code>0 &lt;= matrix[i].length&nbsp;&lt;= 100</code></li> 
</ul>

<p>注意：本题与主站 54 题相同：<a href="https://leetcode-cn.com/problems/spiral-matrix/">https://leetcode-cn.com/problems/spiral-matrix/</a></p>

<div><div>Related Topics</div><div><li>数组</li><li>矩阵</li><li>模拟</li></div></div><br><div><li>👍 520</li><li>👎 0</li></div>
*/

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[] spiralOrder(int[][] matrix) {
        return counterClockwise(matrix);
//        return clockwise(matrix);
    }

    private void printRes(int[] res){
        for(int a=0; a < res.length; a++){
            if(a==0){
                System.out.print("[");
            }
            System.out.print(res[a]);
            if(a != res.length-1) {
                System.out.print(",");
            }else{
                System.out.print("]");
            }
        }
        System.out.println();
    }

    /**
     * 顺时针打印，从左上角开始
     */
    private int[] clockwise(int[][] matrix){
        if(matrix == null || matrix.length == 0){
            return new int[0];
        }

        int rowNum = matrix.length;
        int columnNum = matrix[0].length;
        System.out.println("n:" + rowNum);
        System.out.println("m:" + columnNum);
        System.out.println("test:" + matrix[matrix.length-1][0]);

        int left = 0;
        int right = columnNum - 1;
        int top = 0;
        int bottom = rowNum - 1;

        int[] res = new int[rowNum * columnNum];
        int resIndex = 0;

        while (true) {
            // 从 左 到 右
            for(int i=left; i<=right ; i++){
                res[resIndex++] = matrix[top][i];
            }
            printRes(res);
            if (top++ > bottom) {
                break;
            }
            // 从 上 到 下
            for(int i=top; i<=bottom ; i++){
                res[resIndex++] = matrix[i][right];
            }
            printRes(res);
            if (right-- < left) {
                break;
            }
            // 从 右 到 左
            for (int i=right; i>=left ; i--){
                res[resIndex++] = matrix[bottom][i];
            }
            printRes(res);
            if (bottom-- < top){
                break;
            }
            // 从 下 到 上
            for (int i=bottom; i>=top ; i--){
                res[resIndex++] = matrix[i][left];
            }
            printRes(res);
            if (left++ > right) {
                break;
            }
        }
        return res;
    }

    /**
     * 逆时针打印，从左下角开始
     */
    private int[] counterClockwise(int[][] matrix){
        if (matrix == null || matrix.length == 0) {
            return new int[0];
        }

        // 行
        int n = matrix.length;
        // 列
        int m = matrix[0].length;
        System.out.println("n:" + n);
        System.out.println("m:" + m);
        System.out.println("test:" + matrix[matrix.length-1][0]);

        int top = 0;
        int bottom = n - 1;
        int left = 0;
        int right = m - 1;
        int[] res = new int[n * m];
        int index = 0;
        while (true) {
            // 从 左 到 右
            for (int i = left; i <= right; i++) {
                res[index++] = matrix[bottom][i];
            }
            printRes(res);
            if (top > --bottom) {
                break;
            }
            // 从 下 到 上
            for (int i = bottom; i >= top; i--) {
                res[index++] = matrix[i][right];
            }
            printRes(res);
            if (left > --right) {
                break;
            }
            // 从 右 到 左
            for (int i = right; i >= left; i--) {
                res[index++] = matrix[top][i];
            }
            printRes(res);
            if (++top > bottom) {
                break;
            }
            // 从 上 到 下
            for (int i = top; i <= bottom; i++) {
                res[index++] = matrix[i][left];
            }
            printRes(res);
            if (++left > right) {
                break;
            }
        }
        return res;
    }
}
//leetcode submit region end(Prohibit modification and deletion)
